/*
 *	ME2 C++ SDK
 *	Copyright (c) 2010 _silencer
 *
 *	Permission is hereby granted, free of charge, to any person
 *	obtaining a copy of this software and associated documentation
 *	files (the "Software"), to deal in the Software without
 *	restriction, including without limitation the rights to use,
 *	copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the
 *	Software is furnished to do so, subject to the following
 *	conditions:
 *
 *	The above copyright notice and this permission notice shall be
 *	included in all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *	OTHER DEALINGS IN THE SOFTWARE.
 */

#include "stdafx.h"
#include "check_mod.h"

uengine_interface& engine_interface( uengine_interface::get_singleton() );
boost::shared_ptr<check_mod> check_mod_ptr;

void start_mod()
{
	/* Attach to console of launcher so we can show some information */
	AttachConsole( ATTACH_PARENT_PROCESS );

	*stdout = *_tfdopen( _open_osfhandle( (intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), _O_WRONLY ), _T("a") ); 
	*stderr = *_tfdopen( _open_osfhandle( (intptr_t) GetStdHandle(STD_ERROR_HANDLE), _O_WRONLY ), _T("a") ); 
	*stdin = *_tfdopen( _open_osfhandle( (intptr_t) GetStdHandle(STD_INPUT_HANDLE), _O_WRONLY ), _T("r") ); 

	/* Now setup the sdk and get our mod running */
	std::cout << "check_mod starting..." << std::endl;

	engine_interface.initialize_interface();

	if ( engine_interface.is_initialized() )
		check_mod_ptr.reset( new check_mod() );
}

void stop_mod()
{
	std::cout << "check_mod stopping..." << std::endl;

	if ( check_mod_ptr )
		check_mod_ptr.reset();

	engine_interface.cleanup_interface();
}

check_mod::check_mod()
	: done_check_( false )
{
	std::cout << "check_mod created." << std::endl;
	std::cout << "check_mod is checking the sdk with engine meta data." << std::endl;
}

check_mod::~check_mod()
{
	std::cout << "check_mod is getting destroyed." << std::endl;
}

void check_mod::on_tick( float delta_time )
{
	if ( !done_check_ )
	{
		IMPLEMENT_CHECK_CLASS( uobject )
		IMPLEMENT_CHECK_CLASS( ucomponent )
		IMPLEMENT_CHECK_CLASS( utextbuffer )
		IMPLEMENT_CHECK_CLASS( ufield )
		IMPLEMENT_CHECK_CLASS( uenum )
		IMPLEMENT_CHECK_CLASS( uconst )
		IMPLEMENT_CHECK_CLASS( uproperty )
		IMPLEMENT_CHECK_CLASS( ubyteproperty )
		IMPLEMENT_CHECK_CLASS( uintproperty )
		IMPLEMENT_CHECK_CLASS( uboolproperty )
		IMPLEMENT_CHECK_CLASS( ufloatproperty )
		IMPLEMENT_CHECK_CLASS( uobjectproperty )
		IMPLEMENT_CHECK_CLASS( uclassproperty )
		IMPLEMENT_CHECK_CLASS( ucomponentproperty )
		IMPLEMENT_CHECK_CLASS( unameproperty )
		IMPLEMENT_CHECK_CLASS( ustructproperty )
		IMPLEMENT_CHECK_CLASS( ustrproperty )
		IMPLEMENT_CHECK_CLASS( uarrayproperty )
		IMPLEMENT_CHECK_CLASS( umapproperty )
		IMPLEMENT_CHECK_CLASS( uinterfaceproperty )
		IMPLEMENT_CHECK_CLASS( udelegateproperty )
		IMPLEMENT_CHECK_CLASS( ustringrefproperty )
		IMPLEMENT_CHECK_CLASS( ustruct )
		IMPLEMENT_CHECK_CLASS( ufunction )
		IMPLEMENT_CHECK_CLASS( ustate )
		IMPLEMENT_CHECK_CLASS( uclass )

		done_check_ = true;
	}

	//std::cout << "check_mod tick delta_time = " << delta_time << "." << std::endl;
}

void check_mod::on_post_render( uobject* canvas )
{
	//if ( canvas->is_valid() )
	//	std::cout << "check_mod post_render canvas = " << canvas->get_full_name() << "." << std::endl;
	//else
	//	std::cout << "check_mod post_render canvas = none." << std::endl;
}

void check_mod::on_level_change()
{
	//std::cout << "check_mod detected level change." << std::endl;
}
